package com.gin.kotlin.no_gay_alliance.adapter.user

import android.content.Context
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.databinding.ItemUserBuffBinding
import com.gin.kotlin.no_gay_alliance.model.UserDetailModel
import com.gin.kotlin.no_gay_alliance.utils.TimeUtils.Companion.SIMPLE_DATE_TIME_FORMATTER
import com.gin.nga.enums.UserBuffType
import java.time.ZoneId
import java.time.temporal.ChronoUnit

class UserBuffAdapter(context: Context, handler: MyHandler, data: MutableList<UserDetailModel.UserBuffModel> = mutableListOf()) :
    AbstractBindingRecyclerViewAdapter<ItemUserBuffBinding, UserDetailModel.UserBuffModel>(context, handler, false, data) {
    override fun onBindViewHolder(holder: ViewHolder<ItemUserBuffBinding>, position: Int, b: ItemUserBuffBinding, dataModel: UserDetailModel.UserBuffModel) {
        // 持续时间
        val start = dataModel.start?.let { SIMPLE_DATE_TIME_FORMATTER.format(it.withZoneSameInstant(ZoneId.systemDefault())) }
        val end = dataModel.end?.let { SIMPLE_DATE_TIME_FORMATTER.format(it.withZoneSameInstant(ZoneId.systemDefault())) }
        val days = ChronoUnit.DAYS.between(dataModel.start, dataModel.end)
        b.tvBuffDuration.text = "$start 至 $end"


        val buffType = dataModel.getType()
        val permissionBuffType = dataModel.getPermissionBuffType()

        b.tvBuffDescription.text = when (buffType) {
            UserBuffType.MUTED -> if (dataModel.extraData != null && dataModel.extraData != 0L) {
                "在版面 [${dataModel.forumName}] 禁言 ${days}天"
            } else {
                "在全版面禁言 ${days}天"
            }

            UserBuffType.PERMISSION -> permissionBuffType?.let { "[${it.tag}] ${it.description}" } ?: dataModel.description

            UserBuffType.DISABLE_BUFF -> "[${UserBuffType.DISABLE_BUFF.tag}] ${UserBuffType.DISABLE_BUFF.description}"

            UserBuffType.AVATAR_CHANGED -> run {
                val tag = "[头像改变]"
                val type = App.INSTANCE.avatarBuffs?.firstOrNull { it.id == dataModel.extraData?.toInt() }?.itemTypeModel
                if (type != null) {
                    "$tag ${type.name}:${type.description}"
                } else {
                    "$tag id = ${dataModel.extraData}"
                }
            }

            else -> dataModel.description
        }
    }
}